using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Reflection;
using System.Xml.Serialization;
using System.IO;

namespace Camalon.ColorPicker
{
	/// <summary>
	/// Summary description for ColorDialog.
	/// </summary>
	public class ColorDialog : System.Windows.Forms.Form
	{
		private ColorPicker.ColorSelectionPanelNew _colorSelectionPanel;

		/// <summary>
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.Container components = null;

		private const string _windowTitle = "Camalon Color Picker";


		public ColorDialog()
		{
			InitializeComponent();
            this.Icon = Properties.Resources.App;
			this._colorSelectionPanel.SizeChanged += new System.EventHandler(this.colorSelectionPanel_SizeChanged);

		}

		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if(components != null)
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			this._colorSelectionPanel = new Camalon.ColorPicker.ColorSelectionPanelNew();
			this.SuspendLayout();
			// 
			// _colorSelectionPanel
			// 
			this._colorSelectionPanel.BackColor = System.Drawing.SystemColors.Control;
			this._colorSelectionPanel.Dock = System.Windows.Forms.DockStyle.Fill;
			this._colorSelectionPanel.Location = new System.Drawing.Point(0, 0);
			this._colorSelectionPanel.Name = "_colorSelectionPanel";
			this._colorSelectionPanel.ParentDialog = this;
			this._colorSelectionPanel.Size = new System.Drawing.Size(542, 624);
			this._colorSelectionPanel.TabIndex = 0;
			this._colorSelectionPanel.Cancelled += new System.EventHandler(this.colorSelectionPanel_Cancelled);
			this._colorSelectionPanel.FileChanged += new System.EventHandler(this.colorSelectionPanel_FileChanged);
			this._colorSelectionPanel.OKClicked += new System.EventHandler(this.colorSelectionPanel_OKClicked);
			// 
			// ColorDialog
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
			this.BackColor = System.Drawing.SystemColors.Control;
			this.ClientSize = new System.Drawing.Size(542, 624);
			this.Controls.Add(this._colorSelectionPanel);
			this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog;
			this.MaximizeBox = false;
			this.Name = "ColorDialog";
			this.Text = "Camalon Color Picker";
			this.ResumeLayout(false);

		}
		#endregion


		protected override void OnLoad(EventArgs e)
		{
			base.OnLoad (e);
			Size size = this.ClientSize;
			size.Height = this._colorSelectionPanel.Height+4;
			this.ClientSize = size;



		}

		private void colorSelectionPanel_SizeChanged(object sender, System.EventArgs e)
		{
			if( this.WindowState == FormWindowState.Minimized) return;

			Size size = this.ClientSize;
			size.Height = this._colorSelectionPanel.Height+4;
			this.ClientSize = size;

			
		}

		private void colorSelectionPanel_OKClicked(object sender, System.EventArgs e)
		{
			
			this.DialogResult = DialogResult.OK;
			this.Close();

		}

		private void colorSelectionPanel_Cancelled(object sender, System.EventArgs e)
		{
			this.DialogResult = DialogResult.Cancel;
			this.Close();
		}

		private void colorSelectionPanel_FileChanged(object sender, System.EventArgs e)
		{
			string path = this._colorSelectionPanel.FilePath;
			string text;

			//if( path == null ) text = _windowTitle + " - MyColors.xml";
			if( path == null || path.Length == 0 ) text = _windowTitle;
			else text = _windowTitle+ " - " + Path.GetFileName( path );
			
			this.Text = text;
		}

		#region Public Properties
		[Browsable(false)]
		public string FilePath
		{
			get{ return this._colorSelectionPanel.FilePath;}
		}


		public Color[] GetMyColors(bool sorted)
		{
			
			return this._colorSelectionPanel.GetMyColors(sorted);
		}

		public void SetMyColors(Color[] colors)
		{
			this.SetMyColors(colors, -1);
		}

		public void SetMyColors(Color[] colors, int selectedIndex)
		{
			this._colorSelectionPanel.SetMyColors(colors, selectedIndex);
			if( this.FileOpenOnly ) this.Text = _windowTitle;

		}



		[Browsable( false )]
		public Color PrimaryColor
		{
			get
			{
				Color c = ColorRenderingValue.ARGBColor;

				if( ColorConverter.IsTransparent( c )) return Color.Transparent;
				if( ColorConverter.IsEmpty( c )) return Color.Empty;

				if( c.A == 255 )
				{
					string name = ColorConverter.GetColorName( c.R,c.G, c.B);
					if(name != ColorConverter.UNKNOWN_COLOR_TEXT ) c = Color.FromName( name);
				}

				return c;

			}
			set
			{
				ColorRenderingValue.Opacity = value.A;
				ColorRenderingValue.Color = value;
				this._colorSelectionPanel.SetMySelectedColor( value );
			}
		}


	
		[DefaultValue(false)]
		public bool ShowWheel
		{
			get{ return this._colorSelectionPanel.ShowWheel;}
			set{this._colorSelectionPanel.ShowWheel= value;}
		}



		[DefaultValue(false)]
		public bool ShowOpacityWindow
		{
			get{ return this._colorSelectionPanel.ShowOpacityWindow;}
			set{ this._colorSelectionPanel.ShowOpacityWindow = value;}

		}


		[DefaultValue( true)]
		[RefreshProperties( RefreshProperties.All)]
		public bool AllowFullOpen
		{
			get{ return this._colorSelectionPanel.AllowFullOpen;}
			set{ this._colorSelectionPanel.AllowFullOpen = value;}

		}

		[DefaultValue( true)]
		public bool FullOpen
		{
			get{ return this._colorSelectionPanel.FullOpen;}
			set{ this._colorSelectionPanel.FullOpen = value; }
		}

		[DefaultValue( false )]
		public bool FileOpenOnly
		{
			get{ return this._colorSelectionPanel.FileOpenOnly;}
			set{ this._colorSelectionPanel.FileOpenOnly = value;}
		}


		[DefaultValue( true )]
		public bool AllowMyColors
		{
			get{ return this._colorSelectionPanel.AllowMyColors;}
			set{this._colorSelectionPanel.AllowMyColors=value;}
		}

		[DefaultValue( ListViewStyle.LargeIcon)]
		public ListViewStyle ViewStyle
		{
			get{ return this._colorSelectionPanel.ViewStyle;}
			set{ this._colorSelectionPanel.ViewStyle  = value; }
		}

		[DefaultValue( false)]
		public bool AutoName
		{
			get{ return this._colorSelectionPanel.AutoName;}
			set{ this._colorSelectionPanel.AutoName = value;}
		}


		public bool HasMyListChanges()
		{
			return this._colorSelectionPanel.HasMyListChanges();
		}

		#endregion Public Properties


	}
}
